﻿using System.Configuration;
using Quintsys.Data.Model.Enums;

namespace Quintsys.Core.Services
{
    public interface ISearchParametersService
    {
        /// <summary>
        /// Gets the status filter.
        /// </summary>
        /// <param name="filterName">Name of the filter.</param>
        /// <returns></returns>
        StatusFilter GetStatusFilter(string filterName);

        /// <summary>
        /// Gets the size of the page.
        /// </summary>
        /// <returns></returns>
        int GetPageSize(string pageName);

        /// <summary>
        /// Sets the status filter.
        /// </summary>
        /// <param name="filterName">Name of the filter.</param>
        /// <param name="statusFilter">The status filter.</param>
        void SetStatusFilter(string filterName, StatusFilter statusFilter);
    }

    public class SearchParametersService : ISearchParametersService
    {
        /// <summary>
        /// Gets the status filter.
        /// </summary>
        /// <param name="filterName">Name of the filter.</param>
        /// <returns></returns>
        public StatusFilter GetStatusFilter(string filterName)
        {
            var statusFilter = SessionVariablesService.Get<StatusFilter>(filterName);
            if (statusFilter == StatusFilter.Unknown)
                statusFilter = StatusFilter.All;
            return statusFilter;
        }

        /// <summary>
        /// Gets the size of the page.
        /// </summary>
        /// <param name="pageName"></param>
        /// <returns></returns>
        public int GetPageSize(string pageName)
        {
            var pageSize = SessionVariablesService.Get<int>(pageName);
            if (pageSize == 0)
            {
                if (!int.TryParse(ConfigurationManager.AppSettings["DefaultPageSize"], out pageSize))
                {
                    pageSize = 75;
                }
            }
            return pageSize;
        }

        /// <summary>
        /// Sets the status filter.
        /// </summary>
        /// <param name="filterName">Name of the filter.</param>
        /// <param name="statusFilter">The status filter.</param>
        public void SetStatusFilter(string filterName, StatusFilter statusFilter)
        {
            SessionVariablesService.Set(filterName, statusFilter.ToString());
        }
    }
}
